<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class MjzjReviewer extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * 数据表名
     *
     * @var string
     */
    protected $table = 'mjzj_reviewers';

    /**
     * 可批量赋值的属性
     *
     * @var array<string>
     */
    protected $fillable = [
        'reviewer_id',
        'name',
        'avatar',
        'platform',
        'country',
        'rating',
        'review_count',
        'follower_count',
        'email',
        'contact_info',
        'description',
        'keywords',
        'market',
        'exposure_score',
        'last_active',
        'raw_data',
    ];

    /**
     * 应该被转换为原生类型的属性
     *
     * @var array<string, string>
     */
    protected $casts = [
        'rating' => 'decimal:2',
        'review_count' => 'integer',
        'follower_count' => 'integer',
        'exposure_score' => 'decimal:2',
        'last_active' => 'datetime',
        'raw_data' => 'json',
    ];

    /**
     * 获取评论者的完整信息
     */
    public function getFullInfoAttribute(): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'platform' => $this->platform,
            'rating' => $this->rating,
            'review_count' => $this->review_count,
            'follower_count' => $this->follower_count,
            'exposure_score' => $this->exposure_score,
        ];
    }

    /**
     * 根据评论者ID查找或创建记录
     */
    public static function findOrCreateByReviewerId(string $reviewerId, array $data): self
    {
        return static::firstOrCreate(
            ['reviewer_id' => $reviewerId],
            $data
        );
    }
} 